package site.xuxingcan;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.protocol.HttpContext;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PostRunnable extends Thread {
    private final CloseableHttpClient httpClient;
    private final HttpContext context;
    private final HttpPost httppost;

    public PostRunnable(CloseableHttpClient httpClient, HttpPost httppost) {
        this.httpClient = httpClient;
        this.context = HttpClientContext.create();
        this.httppost = httppost;
    }

    @Override
    public void run() {
        try {
            CloseableHttpResponse response = this.httpClient.execute(this.httppost, this.context);
            try {
                HttpEntity entity = response.getEntity();

                StringBuffer result = new StringBuffer();

                BufferedReader in = new BufferedReader(new InputStreamReader(entity
                        .getContent()));
                String line;
                while ((line = in.readLine()) != null) {
                    result.append(line);
                }
//                System.out.println("result = " + URLDecoder.decode(result.toString()));
//                try {
//                    JSONObject jsonObject = JSONObject.parseObject(URLDecoder.decode(result.toString(), "UTF-8"));
//                    String type = jsonObject.getString("type");
//                    String str1 = jsonObject.getString("message");
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
            } finally {
                response.close();
            }
        } catch (ClientProtocolException localClientProtocolException) {
        } catch (IOException localIOException) {
        }
    }
}